# 1、登录
# 2、添加用户分组——》删除刚才新增的用户分组
# 3、添加是否成功，断言；删除是否成功，断言
# 组件化，元素与脚本分离
import time
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import uuid
from elements import *
class Crmeb:
    _ = str(uuid.uuid4()).split('-')[0]
    def __init__(self,driver,url):
        self.driver = driver
        self.url = url
    def open_url(self):
        """
        打开指定网页
        :return:
        """
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(15)
    def login(self,user,password):
        # 输入用户名
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_password))[0].send_keys(user)
        # 输入密码
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_password))[1].send_keys(password)
        # 点击登录按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(login_btn)).click()
    def add_user_group(self,group_name):
        # 点击左侧列表的用户
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_btn))[1].click()
        # 点击用户分组icon
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(user_group_icon)).click()
        # 点击添加分组
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(click_add_group)).click()
        # 输入分组名称
        time.sleep(2)
        user_group = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(input_group_name))[2]
        # 先清除，再输入
        user_group.clear()
        user_group.send_keys(group_name + str(self._))
        # 点击确定按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept))[2].click()
        # 确定有没有添加成功
        time.sleep(2)  # 等待页面刷新出刚新增的分组
        # find_element
        group = WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located((By.XPATH,f"//*[text()='{group_name}{str(self._)}']"))).text
        # 断言
        assert group == group_name + str(self._)
    def delete_groups(self):
        # 通过获取所有的删除元素，删除最后一个刚新增的
        # find_elements == presence_of_all_elements_located()
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(delete_group))[-1].click()
        # 点击确定按钮
        WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_all_elements_located(click_accept))[2].click()
        time.sleep(5)
if __name__ == '__main__':
    driver = webdriver.Chrome()
    c1 = Crmeb(driver,'http://8.148.229.201:8899/admin')
    c1.open_url()
    c1.login('admin','123456')
    c1.add_user_group("社会琳")
    c1.delete_groups()








# 作业二：
# QQ音乐登录：https://y.qq.com/portal/pop_login.html
#
# 作业三-crmeb：
# 1、添加用户标签——》删除刚才新增的用户标签
# 2、添加是否成功，断言；删除是否成功，断言